/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.gui.jframe;

import com.rh.game.Character;
import com.rh.game.EquipmentChange;
import com.rh.game.World;

import com.rh.gui.Gui;
import com.rh.gui.RenderEvent;

import com.rh.gui.impl.CharacterJPanel;
import com.rh.gui.impl.ConversationJDialog;

import java.awt.BorderLayout;

import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Hayden
 */
public class JaydenJFrame extends JFrame implements Gui {
    private World world = null;
    
    public JaydenJFrame(World pWorld)  {
        super("TG RPG (Proof of Concept) @ 2008-01-16");
        world = pWorld;
    }

    private static void debug(Object o)  {
        // System.out.println("JaydenJFrame: " + o);
    }
    private TileGridJPanel tileGrid = null;
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public void createAndShowGUI() {
        //Create and set up the window.
        initComponents();

        this.addKeyListener(tileGrid);
        
        //Display the window.
        pack();
        setVisible(true);
        begin();
    }
    
    private void initComponents()  {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tileGrid = new TileGridJPanel(this, world);
        getContentPane().add(tileGrid, BorderLayout.CENTER);
        
        JLabel labelHelp = new JLabel("Move: Arrow Keys, W,A,S,D; (C)haracter; (T)alk");
        getContentPane().add(labelHelp, BorderLayout.SOUTH);
    }
    
    private void begin()  {
        List<RenderEvent> events = world.processInput(null);
        this.render(events, world);
    }
    
    public boolean render(final List<RenderEvent> events, final World world)  {
        boolean gameOver = false;

        for (RenderEvent re : events)  {
            debug("  >>> " + re);
            if (re.getType() == RenderEvent.Type.CHAT)  {
                gameOver = chat(re.getID(), world);
                if (gameOver)  break;
            }
            else if (re.getType() == RenderEvent.Type.MALE_TO_FEMALE)  {
                System.out.println(re.getCharacter().getID() + ", " + re.getCharacter().getName());
                JOptionPane.showMessageDialog(null, re.getCharacter().getName() + " HAS BEEN CHANGED INTO A GIRL!", "Male to Female Transformation", JOptionPane.INFORMATION_MESSAGE);
            }
            else if (re.getType() == RenderEvent.Type.GENDER_SWAP)  {
                JOptionPane.showMessageDialog(null, "[Insert impress graphic of Final Swap here]", "Final Swap", JOptionPane.INFORMATION_MESSAGE);
            }
            else if (re.getType() == RenderEvent.Type.GAME_OVER)  {
                JOptionPane.showMessageDialog(null, "The game has ended.", "Game Over", JOptionPane.INFORMATION_MESSAGE);
                gameOver = true;
                break;
            }
        }
        
        tileGrid.repaint();
        
        return gameOver;
    }

    private boolean chat(final String pChatID, final World world) {
        return ConversationJDialog.chat(this, world, pChatID);
    }
    
    public EquipmentChange character(Character whom) {
        return CharacterJPanel.character(whom);
    }
}
